package xy.donate.android.ui.activity

import android.annotation.SuppressLint
import android.text.TextUtils
import android.view.View
import androidx.room.Room
import com.hjq.base.BaseDialog
import kotlinx.android.synthetic.main.activity_add_address.*
import xy.donate.android.R
import xy.donate.android.action.ToastAction
import xy.donate.android.common.MyActivity
import xy.donate.android.constant.Constant
import xy.donate.android.db.AddressDao
import xy.donate.android.db.AddressDb
import xy.donate.android.entity.AddressEntity
import xy.donate.android.helper.MmkvHelper
import xy.donate.android.ui.dialog.AddressDialog

class AddNewAddressActivity : MyActivity(), ToastAction {
    private val address = AddressEntity()

    override fun getLayoutId(): Int {
        return R.layout.activity_add_address
    }

    @SuppressLint("SetTextI18n")
    override fun initView() {
        val addressBean = intent.getSerializableExtra("address") as AddressEntity?
        if (addressBean != null){
            et_name.setText(addressBean.realName)
            tb_men.isChecked = addressBean.sex == 0
            tb_women.isChecked = addressBean.sex == 1
            et_phone.setText(addressBean.phoneNum)
            tv_address.text = addressBean.cityStr
            et_mp.setText(addressBean.floorNum)
        }
        ll_choose_address.setOnClickListener { view: View? ->
            // 选择地区对话框
            AddressDialog.Builder(this)
                    .setTitle(getString(R.string.address_title)) // 设置默认省份
                    .setListener { _: BaseDialog?, province: String, city: String, area: String ->
                        tv_address.text = province + city + area
                    }
                    .show()
        }

        rg_sex.setOnCheckedChangeListener { radioGroup, i ->
            when (i) {
                R.id.tb_men -> {
                    address.sex = 0
                }
                R.id.tb_women -> {
                    address.sex = 1
                }
            }
        }

        add_address.setOnClickListener {
            val name = et_name.text.toString()
            val phone = et_phone.text.toString()
            val mp = et_mp.text.toString()
            val add = tv_address.text.toString()
            if (TextUtils.isEmpty(name)){
                toast("收货人姓名不能为空")
                return@setOnClickListener
            }
            if (TextUtils.isEmpty(phone)){
                toast("收货人手机号不能为空")
                return@setOnClickListener
            }
            if (TextUtils.isEmpty(add)){
                toast("收货地址不能为空")
                return@setOnClickListener
            }
            if (TextUtils.isEmpty(mp)){
                toast("门牌不能为空")
                return@setOnClickListener
            }
            address.cityStr = add
            address.phoneNum = phone
            address.detailAddress = add.plus(mp)
            address.realName = name
            address.floorNum = mp
            address.userID = MmkvHelper.getInstance().mmkv.getString(Constant.USER_ID_NUM, "")!!
            address.addressID = address.userID.plus("timeAddress").plus(Constant.getUUID())
            val db = Room.databaseBuilder(this, AddressDb::class.java, "addressDB")
                    .allowMainThreadQueries()
                    .build()
            db.addressDao().insert(address)
            toast("添加成功")
            finish()
        }
    }

    override fun initData() {

    }
}